<?php
class SF_Album_Block_Mobile extends Mage_Catalog_Block_Product_Abstract
{
    public function newprouduct($page){
        $currpage = !empty($page) ? $page : 1;
        $pagesize = 16;
        $collection = Mage::getModel('catalog/category')
                            ->getProductCollection()
                            ->addAttributeToSelect('*')
                            ->addAttributeToFilter('status', 1)
                            ->addAttributeToFilter('visibility', 4)
                            ->addAttributeToFilter('approval', 2)
                            ->setPageSize(intval($pagesize))
                            ->setCurPage(intval($currpage))
                            ->setOrder('updated_at','desc')
                            ->addAttributeToFilter('updated_at', 
                                array('gteq' => date('Y-m-d H:i:s',strtotime("-10 week"))));
        $product= $this->collection($collection);
        return $product;
    }
    public function bestsalesprouduct($page){
        $currpage = !empty($page) ? $page : 1;
        $pagesize = 16;
        $collection = Mage::getModel('catalog/category')
                            ->getProductCollection()
                            ->addAttributeToSelect('*')
                            ->addAttributeToFilter('status', 1)
                            ->addAttributeToFilter('visibility', 4)
                            ->addAttributeToFilter('approval', 2)
                            ->setPageSize(intval($pagesize))
                            ->setCurPage(intval($page));
        
        $collection->getSelect()->joinLeft(array("sfoi" => 'sales_flat_order_item'),
                                "e.entity_id = sfoi.product_id",
                                array())
                         ->joinLeft(array("sfo" => 'sales_flat_order'),
                                "sfoi.order_id = sfo.entity_id",
                                array())
                         ->where("sfoi.parent_item_id is NUll")
                         // ->where("sfo.status = 'payment_success' or sfo.status = 'pending'")
                         ->columns(array('sfoi_qty_ordered' => new Zend_Db_Expr('sum(sfoi.qty_ordered)')))
                         ->group("e.entity_id")
                         ->order('sfoi_qty_ordered desc');
                         
        $product= $this->collection($collection);
        return $product;
    }
    public function saleoffprouduct($page){
        $currpage = !empty($page) ? $page : 1;
        $pagesize = 16;
        $collection = Mage::getModel('catalog/category')->load($id)
                            ->getProductCollection()
                            ->addAttributeToSelect('*')
                            ->addAttributeToFilter('status', 1)
                            ->addAttributeToFilter('visibility', 4)
                            ->addAttributeToFilter('approval', 2)
                            ->addAttributeToFilter('special_price', array('gt' => 0))
                            ->setPageSize(intval($pagesize))
                            ->setCurPage(intval($currpage));

        $collection->getSelect()->joinLeft(array("cpip" => 'catalog_product_index_price'),
                                     "e.entity_id = cpip.entity_id",
                                     array('discount' => 'round((cpip.price-cpip.final_price)/cpip.price,2)'))
                         ->group('e.entity_id')
                         ->order('discount desc');

        $product= $this->collection($collection);
        return $product;
    }
    public function collection($collection){
        $i = '0';
        $w = '190';
        $h = '190';
        $_collectionSize = $collection->count();
        $productImg = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA).'catalog/product/placeholder/default/placeholder-image-rumall.jpg';
        $products = array();
        if($_collectionSize > 0){
            foreach ($collection as $_product){
                $shortdes = Mage::helper('core')->stripTags($_product->getShortDescription());
                if(mb_strlen($shortdes, 'utf-8')>100) {
                    $shortdes = rtrim(mb_substr($shortdes, 0, 100, 'utf-8')) . '...';
                }
                $clas01 = $i == 0 ?' first':'';
                $clas02 = $i+1 == $_collectionSize ?' last':'';

                $product = '<li class="item '.$clas01.$clas02.'"><div class="product-item">';
                $product = $product.'<div class="product-shop-top">';
                $product = $product.'<a class="product-image" href="'.$_product->getProductUrl().'" title="'.$_product->getName().'"><img class="img-responsive em-alt-hover lazy" src="'. Mage::helper('catalog/image')->init($_product, 'small_image')->resize(190) .'" alt=""/><img class="img-responsive em-alt-org lazy" src="'.Mage::helper('catalog/image')->init($_product,'thumbnail')->resize(190) .'" alt="" id="product-collection-image-'.$_product->getId() .'"/><span class="bkg-hover"></span></a>';

//                $product = $product.'<div class="bottom">';
//                if($_product->isSaleable()){
//                    $product = $product.'<button type="button" title="'. Mage::helper("core")->quoteEscape($this->__("Add to Cart")) .'" class="button btn-cart" onclick="setLocation("'. $this->getAddToCartUrl($_product) .'")"><span><span>'. $this->__("Add to Cart") .'</span></span></button>';
//                }else{
//                    $product = $product.'<p class="availability out-of-stock"><span>'. $this->__("Out of stock") .'</span></p>';
//                }
//                $product = $product.'<ul class="add-to-links"><li><a href="'. $this->helper("wishlist")->getAddUrl($_product) .'" class="link-wishlist" title="'. $this->__("Add to Wishlist") .'">'. $this->__("Add to Wishlist") .'</a></li><li><a href="'. $this->getAddToCompareUrl($_product).'" class="link-compare" title="'. $this->__("Add to Compare") .'">'. $this->__("Add to Compare") .'</a></li></ul>';
//                $product = $product.'</div>';

                $product = $product.'</div>';
                $product = $product.'<div class="product-shop"><div class="f-fix"><h2 class="product-name text-center"><a href="'. $_product->getProductUrl() .'" title="'. $_product->getName() .'">'. $_product->getName() .'</a></h2><div class="text-center">'.$this->getReviewsSummaryHtml($_product, 'short').'</div><div class="text-center">'.$this->getPriceHtml($_product, true).'</div></div></div>';
                $product = $product.'</div></li>';

                $products[]= $product;
                $i++;
                if($i >= $_collectionSize) break;
            }
            $returns['info']='success';
            $returns['product']=$products;
        }else {
            $returns['info']='error';
        }
        return $returns;
    }
}